/**
 * 2011-08-11 角色操作并联
 * huhui
 */

import com.Module.organization.Organization;
import com.Module.prms.operation;
import com.Module.prms.role;
import com.Module.prms.roleEntEvent;
import com.Module.prms.roleoption;

import common.checkbox.MycheckBox;

import flash.events.MouseEvent;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.controls.Label;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

import spark.components.HGroup;
import spark.components.Label;
import spark.components.VGroup;

private function initApp():void
{
	rolels.textInput.editable=false;
	orgsTreeData= index.orgsTreeAc;
	orgsTree.dataProvider = orgsTreeData;
	orgsAction.findById(index.orgid);
}


/**
 * 获取选中项
 */
private function getRole(){
	var orgid : String = orgsTree.treeSelectedItem.id;
//	if(orgPid=="0"){
//		转到溯源组件查询所有流程 
//		tsEntFlow.listFlowEvent(orgid);
//	}
//	else{
		//根据组织id，转到权限组件查询企业设备权限
//		prmsRoleEntEvent.listFlowEvent(orgid);
//	}
	//根据企业获取角色
	roleRemoteobject.getorgrole(orgid);
//	roleobj.orgPid=Pid;
//	roleRemoteobject.list(roleobj,0,10000);
}


private function listHandler(event:ResultEvent):void{
	listData.refresh();
	var page:Object =  event.result as Object;
	
	listData = page.rows as ArrayCollection;
}
private function orgidsHandler(event:ResultEvent):void{
	var org : Organization= event.result as Organization;
	orgPid = org.pid;
	if(orgPid!="0"){
		prmsRoleEntEvent.listFlowEvent(index.roleid);
	}else{
//		转到溯源组件查询所有流程 
		tsEntFlow.listFlowEvent(org.id);
	}

}


private function listmenuoptHandler(event:ResultEvent):void{
	listmenuopt = event.result as ArrayCollection;
	cretecheckbox();
	
}
/**huhui 2011-08-11
 * 动态实例化模块，菜单，操作checkbox
 */

public function cretecheckbox()
{
	mo_view.removeAllElements();
	var pmsize:int=listmenuopt.length;
	for(var i:int=0;i<pmsize;i++)
	{
		var obj:Object = listmenuopt.getItemAt(i);
		var pmlable:spark.components.Label =new spark.components.Label();
		pmlable.text=obj.flowName;
		pmlable.setStyle("fontSize","16");
		pmlable.setStyle("fontWeight","bold");
		pmlable.setStyle("color","red");
		
		var pmHgroup:VGroup = new VGroup();
		pmHgroup.percentWidth=100;
		pmHgroup.percentHeight=20;
		pmHgroup.paddingLeft=30;
		pmHgroup.addElement(pmlable);
		
		var pmcarrc:ArrayCollection = new ArrayCollection;
		if(orgPid=="0"){
			 pmcarrc = obj.tsEntEvents;
		}else{
		pmcarrc = obj.prmsRoleEntEvent;
		}
		
		if(pmcarrc!=null)
		{
			
			var pmcsize:int=pmcarrc.length;
			for(var j:int=0;j<pmcsize;j++)
			{
				var objc:Object=pmcarrc.getItemAt(j);
				objc.flowName = obj.flowName;
				objc.orgId = obj.orgid
				var menuopt:HGroup = new HGroup();
				var cmlabe:spark.components.Label = new spark.components.Label();
				cmlabe.text=objc.eventName;
				var cmlabelCanve:spark.components.BorderContainer = new spark.components.BorderContainer();
				cmlabelCanve.height=20;
				cmlabelCanve.width=200;
				cmlabelCanve.setStyle("borderVisible",false);
				cmlabelCanve.addElement(cmlabe);
				menuopt.addElement(cmlabelCanve);
				
				var optcheckbox:MycheckBox = new MycheckBox();
				optcheckbox.id = objc.id;
				optcheckbox.value=objc;
				optcheckbox.addEventListener(MouseEvent.CLICK,getoptsel);
				menuopt.addElement(optcheckbox);
				if(orgPid=="0"){
					optmap.put(objc.id,optcheckbox);/** 把操作对象放入hashmap */
					optid.push(objc.id);/** 把操作对象id放入Array */
				}else{
					optmap.put(objc.eventId,optcheckbox);/** 把操作对象放入hashmap */
					optid.push(objc.eventId);/** 把操作对象id放入Array */
				}

				pmHgroup.addElement(menuopt);
			}
		}
		mo_view.addElement(pmHgroup);
	}
}


public function getoptobj():void
{
	
	//testopt=optmap.getItem("4028850931ac970a0131ac97622c0002") as MycheckBox;
	//testopt.selected=true;
}
public function getoptsel(evt:MouseEvent):void
{
	var opt:MycheckBox = evt.currentTarget as MycheckBox;
	var obj:Object =opt.value;
	optmap.getItem(obj.coperationId).selected = opt.selected;
}


public function save():void
{
	if(rolels.selectedIndex==-1)
	{
		Alert.show("请选择角色！","提示");
		return;
	}
	var selopt:ArrayCollection = new ArrayCollection();
	var roleid:String = rolels.selectedItem.croleId.toString();
	for each (var item:String in optid) {
		var opttem:MycheckBox=optmap.getItem(item) as MycheckBox;
		if(opttem.selected)
		{
			var entEvent:roleEntEvent=new roleEntEvent;
			entEvent.roleId=roleid;
			entEvent.orgId = (opttem.value).orgId;
			if(orgPid=="0"){
				entEvent.eventId=(opttem.value).id;//操作id
				entEvent.flowId = (opttem.value).flowid;
			}else{
				entEvent.eventId=(opttem.value).eventId;//操作id
				entEvent.flowId = (opttem.value).flowId;
			}
			entEvent.eventName=(opttem.value).eventName;
			entEvent.flowName = (opttem.value).flowName;
			entEvent.updaterid=index.userid;
			selopt.addItem(entEvent);
		}
	}
	if(selopt.length==0)
	{
		var entEvent:roleEntEvent=new roleEntEvent;
		entEvent.roleId=roleid;
		selopt.addItem(entEvent);
	}
	prmsRoleEntEvent.saveEntEvent(selopt,roleid);
	
}

public function saveHandler(event:ResultEvent)
{
	var state:Boolean=event.result.state;
	var msg:String = event.result.msg;
	Alert.show(msg,"提示");
}


/**先把选中状态清空，然后通过数据库数据动态选定 */
public function getroleoptHander(event:ResultEvent)
{
	roleopt = event.result as ArrayCollection;
	
	for each(var optitemid:String in optid)
	{
		optmap.getItem(optitemid).selected=false;
	}
	
	if(roleopt!=null)
	{
		
		for each(var obj:Object in roleopt)
		{
//			Alert.show(optmap.size().toString());
//			optmap.getItem(obj.coperationId)
			if(optmap.getItem(obj.eventId)!=null)
			{
				optmap.getItem(obj.eventId).selected=true;
			}
		}
	}
}


